﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace System {
    public class EmailHepler {
        #region 发送电子邮件
        /// <summary>
        /// 发送邮件方法
        /// </summary>
        /// <param name="mailTo">接收人邮件</param>
        /// <param name="mailTitle">发送邮件标题</param>
        /// <param name="mailContent">发送邮件内容</param>
        /// <returns></returns>
        public static bool SendEmail(string mailTo,string mailTitle,string mailContent) {
            //设置发送方邮件信息，例如：qq邮箱
            string stmpServer = @"smtp.163.com";//smtp服务器地址
            string mailAccount = @"minguiluo@163.com";//邮箱账号
            string pwd = @"TPSDOUOTAVMKSESD";//邮箱密码（qq邮箱此处使用授权码，其他邮箱见邮箱规定使用的是邮箱密码还是授权码）

            //邮件服务设置
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            smtpClient.Host = stmpServer;//指定发送方SMTP服务器
            smtpClient.EnableSsl = true;//使用安全加密连接
            smtpClient.UseDefaultCredentials = true;//不和请求一起发送
            smtpClient.Credentials = new NetworkCredential(mailAccount,pwd);//设置发送账号密码

            MailMessage mailMessage = new MailMessage(mailAccount,mailTo);//实例化邮件信息实体并设置发送方和接收方
            //mailMessage.CC.Add("1114800402@qq.com");
            mailMessage.Subject = mailTitle;//设置发送邮件得标题
            mailMessage.Body = mailContent;//设置发送邮件内容
            mailMessage.BodyEncoding = Encoding.UTF8;//设置发送邮件得编码
            mailMessage.IsBodyHtml = true;//设置标题是否为HTML格式
            mailMessage.Priority = MailPriority.Normal;//设置邮件发送优先级

            try {
                smtpClient.Send(mailMessage);//发送邮件
                Logger.LogInfo(mailAccount + "向" + mailTo + "发送邮件了.");
                return true;
            } catch(Exception ex) {
                Logger.LogError("SendEmail:", ex);
                return false;
            }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FromUser">发件人账号</param>
        /// <param name="FromUserName">发件人名字</param>
        /// <param name="FromUserCode">授权码</param>
        /// <param name="ToUser">收件人账号</param>
        /// <param name="EmailType">邮箱类型，格式：smtp.qq.com  smtp.126.com  smtp.164.com</param>
        /// <param name="EmailTitle">邮箱标题</param>
        /// <param name="EmailContent">邮箱内容</param>
        /// <returns></returns>
        public bool SendEmail(string FromUser,string FromUserName,string FromUserCode,string ToUser,string EmailType,string EmailTitle,string EmailContent) {

            MailMessage mailMsg = new MailMessage();//实例化对象
            mailMsg.From = new MailAddress(FromUser,FromUserName);//源邮件地址和发件人
            mailMsg.To.Add(new MailAddress(ToUser));//收件人地址
            mailMsg.Subject = EmailTitle;//发送邮件的标题
            StringBuilder sb = new StringBuilder();
            sb.Append(EmailContent);
            mailMsg.Body = sb.ToString();//发送邮件的内容
                                         //指定smtp服务地址（根据发件人邮箱指定对应SMTP服务器地址）
            SmtpClient client = new SmtpClient();//格式：smtp.126.com  smtp.164.com
            client.Host = EmailType;
            //要用587端口
            client.Port = 587;//端口
                              //加密
            client.EnableSsl = true;
            //通过用户名和密码验证发件人身份
            client.Credentials = new NetworkCredential(FromUser,FromUserCode);
            try {
                client.Send(mailMsg);   //发送邮件
            } catch(SmtpException ex) {
                Logger.LogError("SendEmail", ex);
                return false;
            }
            return true;
        }
    }
}
